<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <div class="app-space-between">
        <div class="app-space-between-left">
          <el-form-item label="优惠券名称:" prop="couponName">
            <el-input v-model="queryParams.couponName" placeholder="请输入优惠券名称" clearable @clear="handleQuery"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="状态:" prop="couponState">
            <el-select v-model="queryParams.couponState" clearable placeholder="请选择商品分类" @change="handleQuery">
              <el-option label="预置" value="预置" />
              <el-option label="发布" value="发布" />
              <el-option label="失效" value="失效" />
              <el-option label="取消" value="取消" />
            </el-select>
          </el-form-item>
        </div>
        <div class="app-space-between-right">
          <el-form-item>
            <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
            <el-button size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="优惠券名称" align="center" prop="couponName" />
      <el-table-column label="面值（元）" align="center" prop="couponValue" />
      <el-table-column label="类型" align="center" prop="couponType" />
      <el-table-column label="有效期" align="center">
        <template slot-scope="scope">
          <div>{{scope.row.validiteStart}} 至 {{scope.row.validiteEnd}}</div>
        </template>
      </el-table-column>
      <el-table-column label="最低消费额度（元）" align="center" prop="minMonetary" />
      <el-table-column label="状态" align="center" prop="couponState" />
    </el-table>

    <!-- 对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="showEdit" :fullscreen="false" width="860px"
      append-to-body>
      <el-form ref="form" :model="form" :rules="formRules" label-width="120px" label-position="right" v-loading="diLogLoading">
        <el-row>
          <el-col :span="24">
            <el-form-item label="优惠券名称:" prop="couponName">
              <el-input v-model="form.couponName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="优惠券面值:" prop="couponValue">
              <el-input v-model.number="form.couponValue" style="width: 300px;"></el-input>
              <span style="margin-left: 10px;">元</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="优惠券类型:" prop="couponType">
              <el-radio v-model="form.couponType" label="1">满减</el-radio>
              <el-radio v-model="form.couponType" label="0">直减</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最低消费金额:" prop="minMonetary">
              <el-input v-model.number="form.minMonetary" style="width: 220px;"></el-input>
              <span style="margin-left: 10px;">元</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="有效期:" prop="time">
              <el-date-picker v-model="form.time" type="daterange" value-format="yyyy-MM-dd" start-placeholder="开始日期"
                end-placeholder="结束日期" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="是否叠加使用:" prop="issuperposition">
              <el-radio v-model="form.issuperposition" label="0">是</el-radio>
              <el-radio v-model="form.issuperposition" label="1">否</el-radio>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="适用商品:" prop="useGoods">
              <el-radio v-model="form.useGoods" label="0">指定商品不可用</el-radio>
              <el-radio v-model="form.useGoods" label="1">指定商品可用</el-radio>
              <span>商品列表未对接</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="每人限领次数:" prop="limitTimeNum">
              <el-radio v-model="form.limitTimeNum" @change="form.limitTime = ''" label="0">不限</el-radio>
              <el-radio v-model="form.limitTimeNum" label="1">限定</el-radio>
              <el-input v-model.number="form.limitTime" placeholder="次数" name="text-align-right"
                :disabled="form.limitTimeNum != 1" style="width: 100px;"></el-input>
              <span style="margin-left: 10px;">次</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="发放总数:" prop="distributionTimesNum">
              <el-radio v-model="form.distributionTimesNum" @change="form.distributionTimes = ''" label="0">不限
              </el-radio>
              <el-radio v-model="form.distributionTimesNum" label="1">限定</el-radio>
              <el-input v-model.number="form.distributionTimes" placeholder="次数" name="text-align-right"
                :disabled="form.distributionTimesNum != 1" style="width: 100px;"></el-input>
              <span style="margin-left: 10px;">次</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注:">
              <el-input type="textarea" v-model="form.f1"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" v-if="!diLogLoading">
        <el-button type="primary" @click="onSubmit" :loading="onSubmitDis">{{form.couponId?'保 存':'确 定'}}</el-button>
        <el-button @click="cancel" :disabled="onSubmitDis">取 消</el-button>
      </div>
    </el-dialog>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
  import {
    couponList,
    couponAdd,
    couponDelete,
    couponDetails,
    couponEdit
  } from "@/api/coupon/content"
  export default {
    data() {
      return {
        onSubmitDis: false,
        showEdit: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        diLogLoading: false,
        // 总条数
        total: 0,
        // 岗位表格数据
        postList: [],
        // 弹出层标题
        title: "",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          couponName: '',
          couponState: ''
        },
        // 表单参数
        form: {},
        // 表单校验
        formRules: {
          couponName: [{
            required: true,
            message: "请输入优惠卷名称",
            trigger: "blur"
          }],
          couponValue: [{
            required: true,
            message: "请输入优惠卷面值",
            trigger: "blur"
          }],
          couponType: [{
            required: true,
            message: "请选择优惠卷类型",
            trigger: "blur"
          }],
          minMonetary: [{
            required: true,
            message: "请输入最低消费金额",
            trigger: "blur"
          }],
          time: [{
            required: true,
            message: "请选择有效期",
            trigger: "blur"
          }],
          issuperposition: [{
            required: true,
            message: "请选择是否叠加",
            trigger: "blur"
          }],
          limitTimeNum: [{
            required: true,
            message: "请选择不限定或限定，限定请输入次数",
            trigger: "blur"
          }],
          useGoods: [{
            required: true,
            message: "请选择使用商品",
            trigger: "blur"
          }],
          distributionTimesNum: [{
            required: true,
            message: "请选择不限定或限定，限定请输入次数",
            trigger: "blur"
          }]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询岗位列表 */
      getList() {
        this.loading = true;
        couponList(this.queryParams).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });

      },
      cancel() {
        this.showEdit = false
      },
      onSubmit() {
        this.$refs["form"].validate(valid => {
          if (!valid) return
          let obj = {
            ...this.form
          }
          obj.validiteStart = obj.time[0]
          obj.validiteEnd = obj.time[1]
          delete obj.time
          this.onSubmitDis = true
          if (obj.couponId != undefined) {
            couponEdit(obj).then(res => {
              this.getList()
              this.showEdit = false
              this.onSubmitDis = false
              this.$message.success('编辑成功')
            })
          } else {
            couponAdd(obj).then(res => {
              this.getList()
              this.showEdit = false
              this.onSubmitDis = false
              this.$message.success('新增成功')
            })
          }
        })
      },
      // 表单重置
      reset() {
        this.form = {
          couponName: '',
          couponValue: '',
          couponType: '0', //表里也没有注释，先暂定0直减 1满减吧
          minMonetary: '',
          time: '',
          validiteStart: '',
          validiteEnd: '',
          issuperposition: '0',
          limitTime: '',
          limitTimeNum: '0',
          distributionTimes: '',
          distributionTimesNum: '0',
          useGoods: '0',
          f1: ''
        };
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.couponId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.title = '新增'
        this.showEdit = true
        this.reset()
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        let postId = row.postId || this.ids
        if (Array.isArray(postId)) postId = postId.toString()
        this.title = "编辑";
        this.showEdit = true
        this.diLogLoading = true
        couponDetails(postId).then(response => {
          response.data.time = [response.data.validiteStart, response.data.validiteEnd]
          this.form = response.data;
          this.diLogLoading = false
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let postIds = row.postId || this.ids;
        if (Array.isArray(postIds)) postIds = postIds.toString()
        this.$modal.confirm('是否确认删除所选的内容？').then(function() {
          return couponDelete(postIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
</script>
<style lang="scss" scoped>
  ::v-deep input[name="text-align-right"] {
    text-align: right !important;
  }
</style>
